/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.hugo.senai.testes;

import br.com.hugo.senai.dao.Hibernate;
import br.com.hugo.senai.entities.Curso;
import br.com.hugo.senai.entities.Disciplina;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Hugo
 */
public class JBotaoAutomatico extends javax.swing.JFrame {

    /**
     * Creates new form JBotaoAutomatico
     */
    public JBotaoAutomatico() {
        initComponents();

    }
    final static int maxGap = 0;

    int contador = 1;
    List<Disciplina> materias;

    public void fillItensEmBotoes() {
        if (contador == 1) {
            GridLayout layout = new GridLayout(0, 2);

            layout.setHgap(2);
            layout.setVgap(4);

            labelItens.setLayout(layout);

//        JButton b = new JButton("Just fake button");
//     //   b.setSize(WIDTH, WIDTH);
//        
//        Dimension buttonSize = b.getPreferredSize();
//        labelItens.setPreferredSize(new Dimension((int) (buttonSize.getWidth() * 2.5) + maxGap,
//                (int) (buttonSize.getHeight() * 3.5) + maxGap * 2));
//        
            labelItens.setPreferredSize(new Dimension((int) 370, (int) 170));

            List<Curso> cursos = Hibernate.listAll(Curso.class);

            int i = 1;
            for (final Curso curso : cursos) {

                materias = Hibernate.listAll(Disciplina.class, curso, "curso");
                labelItens.add(new JButton(new AbstractAction(curso.getNome()) {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        String texto = "";
                        materias = Hibernate.listAll(Disciplina.class, curso, "curso");
                        for (Disciplina materia : materias) {
                            texto += materia.getNome() + "\n";
                        }

                        JOptionPane.showMessageDialog(null, texto);
                        //texto = "";
                    }
                }) {

                    @Override
                    public void setBackground(Color bg) {
                        if (materias.size() >= 2) {
                            super.setBackground(Color.GREEN); //To change body of generated methods, choose Tools | Templates.
                        } else {
                            super.setBackground(Color.RED);
                        }
                    }

                });

                i++;
            }
        } else {

        }
//        pane.add(labelItens, BorderLayout.NORTH);
//        pane.add(new JSeparator(), BorderLayout.CENTER);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelItens = new javax.swing.JPanel();
        b = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout labelItensLayout = new javax.swing.GroupLayout(labelItens);
        labelItens.setLayout(labelItensLayout);
        labelItensLayout.setHorizontalGroup(
            labelItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 373, Short.MAX_VALUE)
        );
        labelItensLayout.setVerticalGroup(
            labelItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 169, Short.MAX_VALUE)
        );

        b.setText("Pesquisar");
        b.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(b, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelItens, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(b, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelItens, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(418, 342));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void bActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bActionPerformed

        //JBotaoAutomatico botao = new JBotaoAutomatico();
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Set up the content pane.
        if (contador == 1) {
            fillItensEmBotoes();
            //Display the window.

            this.repaint();

            this.pack();
            contador = 2;
        } else {
            labelItens.removeAll();
            this.repaint();
            this.pack();
            contador = 1;
        }
        //botao.setVisible(true);
    }//GEN-LAST:event_bActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JBotaoAutomatico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JBotaoAutomatico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JBotaoAutomatico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JBotaoAutomatico.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JBotaoAutomatico().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b;
    private javax.swing.JPanel labelItens;
    // End of variables declaration//GEN-END:variables
}
